package com.ourteam.product.model.dao.impl;

import com.ourteam.product.model.dao.IProductModelPropertyDAO;
import com.ourteam.product.model.dao.ProductModelProperty;
import com.ourteam.product.model.dao.ProductModelPropertyDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 IRKT 2017
 * Auto Generate Date : 2017-11-17 11:01:15
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractProductModelPropertyDAOJdbcImpl extends AbstractSpringDAO
    implements IProductModelPropertyDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, ObjectId, PropertyCode, PropertyName, DataTypeId, DataLength,
            DataAccuracy, IsArray, IsAllowNull, IsPrimaryKey, IsAutoInc,
            SortIndex, Status, Remarks, DataType, DefaultValue, IsUnique,
            SubmitterId, CreatorName, CreateTime, LastModifierName,
            LastModifyTime, ObjectName, ObjectCode, ObjectType
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.ObjectId,
            _DataObjectProperty.PropertyCode, _DataObjectProperty.PropertyName,
            _DataObjectProperty.DataTypeId, _DataObjectProperty.DataLength,
            _DataObjectProperty.DataAccuracy, _DataObjectProperty.IsArray,
            _DataObjectProperty.IsAllowNull, _DataObjectProperty.IsPrimaryKey,
            _DataObjectProperty.IsAutoInc, _DataObjectProperty.SortIndex,
            _DataObjectProperty.Status, _DataObjectProperty.Remarks,
            _DataObjectProperty.DataType, _DataObjectProperty.DefaultValue,
            _DataObjectProperty.IsUnique, _DataObjectProperty.SubmitterId,
            _DataObjectProperty.CreatorName, _DataObjectProperty.CreateTime,
            _DataObjectProperty.LastModifierName,
            _DataObjectProperty.LastModifyTime, _DataObjectProperty.ObjectName,
            _DataObjectProperty.ObjectCode, _DataObjectProperty.ObjectType
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {  };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<ProductModelProperty, ProductModelPropertyDAOQueryBean> daoHelper;

    public AbstractProductModelPropertyDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<ProductModelProperty, ProductModelPropertyDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.product.model.dao.IProductModelObjectDAO.EntityName)
                                .append(" PRODUCT_MODEL_OBJECT ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.product.model.dao.IProductModelObjectDAO.Id,
                    com.ourteam.product.model.dao.IProductModelObjectDAO.EntityName +
                    ".", "PRODUCT_MODEL_OBJECT."));
            relatedJoinSQLBuffer.append(" = ").append(ObjectId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aProductModelProperty
         * @throws Exception
         */
    public boolean checkIsUnique(ProductModelProperty aProductModelProperty)
        throws Exception {
        if (aProductModelProperty == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aProductModelProperty.getId());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aProductModelProperty
     * @throws Exception
     */
    public void insert(ProductModelProperty aProductModelProperty)
        throws Exception {
        super.doInsert(aProductModelProperty);
    }

    /**
         * Batch Insert Datas
         * @param aProductModelProperty
         * @throws Exception
         */
    public void batchInsert(ProductModelProperty[] aProductModelPropertys)
        throws Exception {
        super.doBatchInsert(aProductModelPropertys);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(ProductModelPropertyDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(ProductModelProperty aProductModelProperty)
        throws Exception {
        delete(aProductModelProperty.getId());
    }

    /**
     * Update Data Selective
     * @param aProductModelProperty
     * @throws Exception
     */
    public void update(ProductModelProperty aProductModelProperty)
        throws Exception {
        if (aProductModelProperty.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aProductModelProperty);

        ProductModelProperty tempObj = findById(aProductModelProperty.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aProductModelProperty, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aProductModelPropertys[]
     * @throws Exception
     */
    public void batchUpdate(ProductModelProperty[] aProductModelPropertys)
        throws Exception {
        super.doBatchUpdate(aProductModelPropertys);
    }

    /**
         * Update Data By Condictions
         * @param aProductModelProperty
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(ProductModelProperty aProductModelProperty,
        ProductModelPropertyDAOQueryBean aQueryBean) throws Exception {
        super.doUpdateByQuery(aProductModelProperty, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(ProductModelPropertyDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(ProductModelProperty aProductModelProperty)
        throws Exception {
        if (aProductModelProperty.getPropertyCode() == null) {
            throw new Exception(
                "The ProductModelProperty.propertyCode value is not allow NULL");
        }

        if (aProductModelProperty.getPropertyName() == null) {
            throw new Exception(
                "The ProductModelProperty.propertyName value is not allow NULL");
        }

        if (aProductModelProperty.getIsArray() == null) {
            throw new Exception(
                "The ProductModelProperty.isArray value is not allow NULL");
        }

        if (aProductModelProperty.getIsAllowNull() == null) {
            throw new Exception(
                "The ProductModelProperty.isAllowNull value is not allow NULL");
        }

        if (aProductModelProperty.getIsPrimaryKey() == null) {
            throw new Exception(
                "The ProductModelProperty.isPrimaryKey value is not allow NULL");
        }

        if (aProductModelProperty.getIsAutoInc() == null) {
            throw new Exception(
                "The ProductModelProperty.isAutoInc value is not allow NULL");
        }

        if (aProductModelProperty.getStatus() == null) {
            throw new Exception(
                "The ProductModelProperty.status value is not allow NULL");
        }

        if (aProductModelProperty.getRemarks() == null) {
            throw new Exception(
                "The ProductModelProperty.remarks value is not allow NULL");
        }

        if (aProductModelProperty.getObjectName() == null) {
            throw new Exception(
                "The ProductModelProperty.objectName value is not allow NULL");
        }

        if (aProductModelProperty.getObjectCode() == null) {
            throw new Exception(
                "The ProductModelProperty.objectCode value is not allow NULL");
        }

        if (aProductModelProperty.getObjectType() == null) {
            throw new Exception(
                "The ProductModelProperty.objectType value is not allow NULL");
        }

        if (aProductModelProperty.getDataType() == null) {
            throw new Exception(
                "The ProductModelProperty.dataType value is not allow NULL");
        }

        if (aProductModelProperty.getDefaultValue() == null) {
            throw new Exception(
                "The ProductModelProperty.defaultValue value is not allow NULL");
        }

        if (aProductModelProperty.getIsUnique() == null) {
            throw new Exception(
                "The ProductModelProperty.isUnique value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public ProductModelProperty findById(final long id)
        throws Exception {
        ProductModelPropertyDAOQueryBean queryBean = new ProductModelPropertyDAOQueryBean();

        ProductModelPropertyDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        ProductModelProperty[] ProductModelPropertys = query(queryBean);

        if (ProductModelPropertys.length == 0) {
            return null;
        } else {
            return ProductModelPropertys[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return ProductModelProperty[]
         * @throws Exception
         */
    public ProductModelProperty[] query(
        ProductModelPropertyDAOQueryBean queryBean) throws Exception {
        List result = super.doQuery(queryBean,
                new JdbcProductModelPropertyRowMapper());

        return (ProductModelProperty[]) result.toArray(new ProductModelProperty[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(
        ProductModelPropertyDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(ProductModelPropertyDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(ProductModelProperty[] aProductModelPropertys,
        ProductModelPropertyDAOQueryBean queryBean) throws Exception {
        daoHelper.synchronize(aProductModelPropertys, queryBean);
    }

    public void synchronize(ProductModelProperty[] aProductModelPropertys,
        ProductModelPropertyDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<ProductModelProperty> callBack)
        throws Exception {
        daoHelper.synchronize(aProductModelPropertys, queryBean, callBack);
    }

    public static class JdbcProductModelPropertyRowMapper implements RowMapper<ProductModelProperty> {
        public ProductModelProperty mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            ProductModelProperty object = new ProductModelProperty();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object objectId = null;

                objectId = resultSet.getObject(columnIndex++);

                object.setObjectIdNull(resultSet.getBoolean(columnIndex++));

                if (objectId != null) {
                    setBeanValue(object, _DataObjectProperty.ObjectId, objectId);
                }

                Object propertyCode = null;

                propertyCode = resultSet.getObject(columnIndex++);

                object.setPropertyCodeNull(resultSet.getBoolean(columnIndex++));

                if (propertyCode != null) {
                    setBeanValue(object, _DataObjectProperty.PropertyCode,
                        propertyCode);
                }

                Object propertyName = null;

                propertyName = resultSet.getObject(columnIndex++);

                object.setPropertyNameNull(resultSet.getBoolean(columnIndex++));

                if (propertyName != null) {
                    setBeanValue(object, _DataObjectProperty.PropertyName,
                        propertyName);
                }

                Object dataTypeId = null;

                dataTypeId = resultSet.getObject(columnIndex++);

                object.setDataTypeIdNull(resultSet.getBoolean(columnIndex++));

                if (dataTypeId != null) {
                    setBeanValue(object, _DataObjectProperty.DataTypeId,
                        dataTypeId);
                }

                Object dataLength = null;

                dataLength = resultSet.getObject(columnIndex++);

                object.setDataLengthNull(resultSet.getBoolean(columnIndex++));

                if (dataLength != null) {
                    setBeanValue(object, _DataObjectProperty.DataLength,
                        dataLength);
                }

                Object dataAccuracy = null;

                dataAccuracy = resultSet.getObject(columnIndex++);

                object.setDataAccuracyNull(resultSet.getBoolean(columnIndex++));

                if (dataAccuracy != null) {
                    setBeanValue(object, _DataObjectProperty.DataAccuracy,
                        dataAccuracy);
                }

                Object isArray = null;

                isArray = resultSet.getObject(columnIndex++);

                object.setIsArrayNull(resultSet.getBoolean(columnIndex++));

                if (isArray != null) {
                    setBeanValue(object, _DataObjectProperty.IsArray, isArray);
                }

                Object isAllowNull = null;

                isAllowNull = resultSet.getObject(columnIndex++);

                object.setIsAllowNullNull(resultSet.getBoolean(columnIndex++));

                if (isAllowNull != null) {
                    setBeanValue(object, _DataObjectProperty.IsAllowNull,
                        isAllowNull);
                }

                Object isPrimaryKey = null;

                isPrimaryKey = resultSet.getObject(columnIndex++);

                object.setIsPrimaryKeyNull(resultSet.getBoolean(columnIndex++));

                if (isPrimaryKey != null) {
                    setBeanValue(object, _DataObjectProperty.IsPrimaryKey,
                        isPrimaryKey);
                }

                Object isAutoInc = null;

                isAutoInc = resultSet.getObject(columnIndex++);

                object.setIsAutoIncNull(resultSet.getBoolean(columnIndex++));

                if (isAutoInc != null) {
                    setBeanValue(object, _DataObjectProperty.IsAutoInc,
                        isAutoInc);
                }

                Object sortIndex = null;

                sortIndex = resultSet.getObject(columnIndex++);

                object.setSortIndexNull(resultSet.getBoolean(columnIndex++));

                if (sortIndex != null) {
                    setBeanValue(object, _DataObjectProperty.SortIndex,
                        sortIndex);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object remarks = null;

                remarks = resultSet.getObject(columnIndex++);

                object.setRemarksNull(resultSet.getBoolean(columnIndex++));

                if (remarks != null) {
                    setBeanValue(object, _DataObjectProperty.Remarks, remarks);
                }

                Object dataType = null;

                dataType = resultSet.getObject(columnIndex++);

                object.setDataTypeNull(resultSet.getBoolean(columnIndex++));

                if (dataType != null) {
                    setBeanValue(object, _DataObjectProperty.DataType, dataType);
                }

                Object defaultValue = null;

                defaultValue = resultSet.getObject(columnIndex++);

                object.setDefaultValueNull(resultSet.getBoolean(columnIndex++));

                if (defaultValue != null) {
                    setBeanValue(object, _DataObjectProperty.DefaultValue,
                        defaultValue);
                }

                Object isUnique = null;

                isUnique = resultSet.getObject(columnIndex++);

                object.setIsUniqueNull(resultSet.getBoolean(columnIndex++));

                if (isUnique != null) {
                    setBeanValue(object, _DataObjectProperty.IsUnique, isUnique);
                }

                Object submitterId = null;

                submitterId = resultSet.getObject(columnIndex++);

                object.setSubmitterIdNull(resultSet.getBoolean(columnIndex++));

                if (submitterId != null) {
                    setBeanValue(object, _DataObjectProperty.SubmitterId,
                        submitterId);
                }

                Object creatorName = null;

                creatorName = resultSet.getObject(columnIndex++);

                object.setCreatorNameNull(resultSet.getBoolean(columnIndex++));

                if (creatorName != null) {
                    setBeanValue(object, _DataObjectProperty.CreatorName,
                        creatorName);
                }

                Object createTime = null;

                __dataType = resultSet.getMetaData().getColumnType(columnIndex);

                if ((__dataType == java.sql.Types.DATE) ||
                        (__dataType == java.sql.Types.TIME) ||
                        (__dataType == java.sql.Types.TIMESTAMP)) {
                    createTime = resultSet.getTimestamp(columnIndex++);
                } else {
                    createTime = resultSet.getObject(columnIndex++);
                }

                object.setCreateTimeNull(resultSet.getBoolean(columnIndex++));

                if (createTime != null) {
                    setBeanValue(object, _DataObjectProperty.CreateTime,
                        createTime);
                }

                Object lastModifierName = null;

                lastModifierName = resultSet.getObject(columnIndex++);

                object.setLastModifierNameNull(resultSet.getBoolean(
                        columnIndex++));

                if (lastModifierName != null) {
                    setBeanValue(object, _DataObjectProperty.LastModifierName,
                        lastModifierName);
                }

                Object lastModifyTime = null;

                __dataType = resultSet.getMetaData().getColumnType(columnIndex);

                if ((__dataType == java.sql.Types.DATE) ||
                        (__dataType == java.sql.Types.TIME) ||
                        (__dataType == java.sql.Types.TIMESTAMP)) {
                    lastModifyTime = resultSet.getTimestamp(columnIndex++);
                } else {
                    lastModifyTime = resultSet.getObject(columnIndex++);
                }

                object.setLastModifyTimeNull(resultSet.getBoolean(columnIndex++));

                if (lastModifyTime != null) {
                    setBeanValue(object, _DataObjectProperty.LastModifyTime,
                        lastModifyTime);
                }

                Object objectName = null;

                objectName = resultSet.getObject(columnIndex++);

                object.setObjectNameNull(resultSet.getBoolean(columnIndex++));

                if (objectName != null) {
                    setBeanValue(object, _DataObjectProperty.ObjectName,
                        objectName);
                }

                Object objectCode = null;

                objectCode = resultSet.getObject(columnIndex++);

                object.setObjectCodeNull(resultSet.getBoolean(columnIndex++));

                if (objectCode != null) {
                    setBeanValue(object, _DataObjectProperty.ObjectCode,
                        objectCode);
                }

                Object objectType = null;

                objectType = resultSet.getObject(columnIndex++);

                object.setObjectTypeNull(resultSet.getBoolean(columnIndex++));

                if (objectType != null) {
                    setBeanValue(object, _DataObjectProperty.ObjectType,
                        objectType);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
